package com.rayo.accesscontrollercopy.module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.model.db.bean.KeyBoxData;

import java.util.List;


/**
 * Created by zxy on 2018/4/16.
 */

public class KeyBoxAdapter extends RecyclerView.Adapter<KeyBoxAdapter.ViewHolder> {
    private List<KeyBoxData> mDatas;
    private Context context;
    public KeyBoxAdapter(List<KeyBoxData> datas, Context context) {
        this.mDatas = datas;
        this.context = context;
    }

    @Override
    public KeyBoxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        KeyBoxAdapter.ViewHolder holder = new KeyBoxAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_setbox_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(KeyBoxAdapter.ViewHolder holder, final int position) {
        holder.mKeyboxName.setText(mDatas.get(position).getKeyboxName());
        holder.aSwitch.setChecked(mDatas.get(position).isIsopen());
        if (mDatas.get(position).isIsopen()==true){
//            holder.aSwitch.setText();
        }
        holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mDatas.get(position).setState(b);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mKeyboxName;
        Switch aSwitch;

        public ViewHolder(View view) {
            super(view);
            mKeyboxName = (TextView) view.findViewById(R.id.relay_name);
            aSwitch = (Switch)view.findViewById(R.id.switch_on);
        }
    }
}
